package com.weisy.signin.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.weisy.signin.R;

import androidx.annotation.NonNull;

/**
 * @author weisy
 * @function 选择相册对话框
 * @date 2019/3/23 11:46
 */
public class AlbumDialog extends Dialog {
    private AlbumDialog(@NonNull Context context) {
        super(context, R.style.styleDialog);
    }

    public static class Builder {
        private Context context;

        private DialogInterface.OnClickListener onAlbumListener;
        private DialogInterface.OnClickListener onCameraListener;
        private DialogInterface.OnClickListener onCancelListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setOnAlbumListener(OnClickListener onAlbumListener) {
            this.onAlbumListener = onAlbumListener;
            return this;
        }

        public Builder setOnCameraListener(OnClickListener onCameraListener) {
            this.onCameraListener = onCameraListener;
            return this;
        }

        public Builder setOnCancelListener(OnClickListener onCancelListener) {
            this.onCancelListener = onCancelListener;
            return this;
        }

        public AlbumDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.dialog_choice_album, null);
            final AlbumDialog dialog = new AlbumDialog(context);
            dialog.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            Window window = dialog.getWindow();
            if (null != window) {
                window.setGravity(Gravity.BOTTOM);
                WindowManager.LayoutParams params = window.getAttributes();
                params.width = WindowManager.LayoutParams.MATCH_PARENT;
                params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                window.setAttributes(params);
            }
            view.findViewById(R.id.txt_dg_album).setOnClickListener(v -> {
                if (null != onAlbumListener) {
                    onAlbumListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                } else {
                    dialog.dismiss();
                }
            });
            view.findViewById(R.id.txt_dg_camera).setOnClickListener(v -> {
                if (null != onCameraListener) {
                    onCameraListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                } else {
                    dialog.dismiss();
                }
            });
            view.findViewById(R.id.txt_dg_cancel).setOnClickListener(v -> {
                if (null != onCancelListener) {
                    onCancelListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                } else {
                    dialog.dismiss();
                }
            });
            return dialog;
        }
    }
}
